class Solution(object):
    def longestConsecutive(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        m = {}
        counted = {}
        max_len = 0
        for n in nums:
            m[n] = 1
        for n in m.keys():
            if n in counted:
                continue
            count = 1
            counted[n] = 1
            up = n - 1
            while up in m:
                count += 1
                counted[up] = 1
                up -= 1
            down = n + 1
            while down in m:
                count += 1
                counted[down] = 1
                down += 1
            if count > max_len:
                max_len = count
        return max_len


s = Solution()
nums = [100,4,200,1,3,2]
result = s.longestConsecutive(nums)
print(result)

nums = [0,3,7,2,5,8,4,6,0,1]
result = s.longestConsecutive(nums)
print(result)

nums = []
result = s.longestConsecutive(nums)
print(result)

nums = [-1,-9,-5,-2,-9,8,-8,1,-9,-3,-3]
result = s.longestConsecutive(nums)
print(result)
